#include "GQueryMgr.h"
#include "Character/Player.h"
#include "Character/PlayerMgr.h"
#include "LogicService.h"
#include "Common/TeamDefine.h"
#include "Character/PackagePart.h"



using namespace ProtoBuf;

GQueryManager::GQueryManager()
{

}

GQueryManager::~GQueryManager()
{

}

bool GQueryManager::Init()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return false;
	}
	pMessageDispatch->registerServerHandler(EMODULE_ID_QUERY, this);

	return true;
}

void GQueryManager::UnInit()
{
	IMessageDispatch *pMessageDispatch = g_GetLogicService()->GetServiceMessageDispatch();
	if (nullptr == pMessageDispatch)
	{
		return;
	}
	pMessageDispatch->unregisterServerHandler(EMODULE_ID_QUERY);
}

void GQueryManager::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	switch (actionId)
	{
	case CENTER_TO_LOGIC_CHARACTER_DETAIL_DATA:
	{
		OnCharacterDetailData(package);
		break;
	}
	//case CENTER_TO_LOGIC_LEADER_POS_INFO:
	//{
	//	OnLeaderPosInfo(package);
	//	break;
	//}
	default:
		break;
	}
}

bool GQueryManager::OnCharacterDetailData(RECV_PACKAGE &package)
{
	WGCharacterDetailReq req;
	GWCharacterDetailRsp rsp;

	if (!req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead))
	{
		MMOLOG_FMT_ERROR("[logic] GQueryManager::OnCharacterDetailData WGCharacterDetailReq parse failed...");
		return false;
	}

	CharIDType srcId = req.srcid();
	CharIDType dstId = req.dstid();
	uint32_t type = req.type();

	WCCharacterDetailRsp* detailRsp = rsp.mutable_rsp();
	detailRsp->set_type(type);
	detailRsp->set_ret(RET_SUCCESS);
	rsp.set_srcid(srcId);

	for (uint32_t i = CQTE_ROLE_TYPE; i <= CQTE_PET_TYPE; i++)
	{
		if ((type >> i) & 0x0001)
		{
			switch (i)
			{
			case CQTE_ROLE_TYPE:
			{
				QueryCharacterdDetailInfo(srcId, dstId, *detailRsp);
			}
			break;
			case CQTE_PET_TYPE:
			{
				QueryCharacterdPetInfo(srcId, dstId, *detailRsp);
			}
			break;
			default:
				break;
			}
		}
	}

	g_GetLogicService()->SendDataToCenter(EMODULE_ID_QUERY, LOGIC_TO_CENTER_CHARACTER_DETAIL_DATA, &rsp);
	return true;
}

bool GQueryManager::QueryCharacterdDetailInfo(CharIDType srcId, CharIDType dstId, WCCharacterDetailRsp& rsp)
{
	Player* pPlayer = g_GetCreatureMgr()->GetPlayer(dstId);
	if (nullptr == pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] GQueryManager::QueryCharacterdDetailInfo can not find player playerid:%d", dstId);
		return false;
	}

	PackagePart* pPackage = dynamic_cast<PackagePart*>(pPlayer->GetPart(PART_PACKAGE));
	if (nullptr == pPackage)
	{
		MMOLOG_FMT_ERROR("[logic] GQueryManager::QueryCharacterdDetailInfo can not find package part playerid:%d", dstId);
		return false;
	}

	QueryCharacterDetailInfo* detailInfo = rsp.mutable_characterinfo();
	detailInfo->set_fightpower(pPlayer->GetAttr(C_FIGHT_POWER));
	detailInfo->set_name(pPlayer->GetName());
	detailInfo->set_level(pPlayer->GetAttr(C_LEVEL));
	detailInfo->set_id(dstId);
	detailInfo->set_prof(pPlayer->GetAttr(C_PROF));
	detailInfo->set_title(pPlayer->GetAttr(C_USE_TITLE));
	detailInfo->set_official(pPlayer->GetAttr(C_OFFICIAL));
	detailInfo->set_knight(pPlayer->GetAttr(C_KNIGHT));
	detailInfo->set_contribute(pPlayer->GetAttr(C_CAMP_SCORE));
	detailInfo->set_prestige(pPlayer->GetAttr(C_CAMP_PRESTIGE));
	detailInfo->set_camp(pPlayer->GetAttr(C_CAMP_ID));
	detailInfo->set_guildname(pPlayer->UnionName());
	detailInfo->set_hp(pPlayer->GetAttr(C_HP));
	detailInfo->set_maxhp(pPlayer->GetAttr(C_MAX_HP));
	detailInfo->set_mp(pPlayer->GetAttr(C_MP));
	detailInfo->set_maxmp(pPlayer->GetAttr(C_MAX_MP));
	detailInfo->set_exp(pPlayer->GetAttr(C_EXP));
	detailInfo->set_power(pPlayer->GetAttr(C_POWER));
	detailInfo->set_intelligence(pPlayer->GetAttr(C_INTELLIGENCE));
	detailInfo->set_agility(pPlayer->GetAttr(C_AGILITY));
	detailInfo->set_physique(pPlayer->GetAttr(C_PHYSIQUE));
	detailInfo->set_endurance(pPlayer->GetAttr(C_ENDURANCE));
	detailInfo->set_precision(pPlayer->GetAttr(C_PRECISION));
	detailInfo->set_ap(pPlayer->GetAttr(C_AP));
	detailInfo->set_mat(pPlayer->GetAttr(C_MAT));
	detailInfo->set_dp(pPlayer->GetAttr(C_DP));
	detailInfo->set_mdp(pPlayer->GetAttr(C_MDP));
	detailInfo->set_phyhit(pPlayer->GetAttr(C_PHYHIT));
	detailInfo->set_phydodge(pPlayer->GetAttr(C_PHYDODGE));
	detailInfo->set_phycrit(pPlayer->GetAttr(C_PHYCRIT));
	detailInfo->set_magcrit(pPlayer->GetAttr(C_MAGCRIT));
	detailInfo->set_phytoughness(pPlayer->GetAttr(C_PHYTOUGHNESS));
	detailInfo->set_magtoughness(pPlayer->GetAttr(C_MAGTOUGHNESS));
	detailInfo->set_phycritmul(pPlayer->GetAttr(C_PHYCRITMUL));
	detailInfo->set_magcritmul(pPlayer->GetAttr(C_MAGCRITMUL));
	detailInfo->set_fireatt(pPlayer->GetAttr(C_FIREATT));
	detailInfo->set_firedef(pPlayer->GetAttr(C_FIREDEF));
	detailInfo->set_icedatt(pPlayer->GetAttr(C_ICEATT));
	detailInfo->set_iceddef(pPlayer->GetAttr(C_ICEDEF));
	detailInfo->set_windatt(pPlayer->GetAttr(C_WINDATT));
	detailInfo->set_winddef(pPlayer->GetAttr(C_WINDDEF));
	detailInfo->set_solatt(pPlayer->GetAttr(C_SOLATT));
	detailInfo->set_soldef(pPlayer->GetAttr(C_SOLDEF));
	detailInfo->set_rayatt(pPlayer->GetAttr(C_RAYATT));
	detailInfo->set_raydef(pPlayer->GetAttr(C_RAYDEF));
	detailInfo->set_poisonatt(pPlayer->GetAttr(C_POISONATT));
	detailInfo->set_poisondef(pPlayer->GetAttr(C_POISONDEF));
	detailInfo->set_phycritextra(pPlayer->GetAttr(C_PHYCRITEXTRA));
	detailInfo->set_phycritreduce(pPlayer->GetAttr(C_PHYCRITREDUCE));
	detailInfo->set_phyextradamage(pPlayer->GetAttr(C_PHYEXTRADAMAGE));
	detailInfo->set_phyreducedamage(pPlayer->GetAttr(C_PHYREDUCEDAMAGE));
	detailInfo->set_magcritextra(pPlayer->GetAttr(C_MAGCRITEXTRA));
	detailInfo->set_magcritreduce(pPlayer->GetAttr(C_MAGCRITREDUCE));
	detailInfo->set_magextradamage(pPlayer->GetAttr(C_MAGEXTRADAMAGE));
	detailInfo->set_magreducedamage(pPlayer->GetAttr(C_MAGREDUCEDAMAGE));
	detailInfo->set_controldawn(pPlayer->GetAttr(C_CONTROLDAWN));
	detailInfo->set_persistdown(pPlayer->GetAttr(C_PERSISTDOWN));
	detailInfo->set_weakdown(pPlayer->GetAttr(C_WEAKDOWN));
	detailInfo->set_displacedown(pPlayer->GetAttr(C_DISPLACEDOWN));
	detailInfo->set_silencedown(pPlayer->GetAttr(C_SILENCEDOWN));
	detailInfo->set_controlup(pPlayer->GetAttr(C_CONTROLUP));
	detailInfo->set_persistup(pPlayer->GetAttr(C_PERSISTUP));
	detailInfo->set_weakup(pPlayer->GetAttr(C_WEAKUP));
	detailInfo->set_displaceup(pPlayer->GetAttr(C_DISPLACEUP));
	detailInfo->set_silenceup(pPlayer->GetAttr(C_SILENCEUP));
	detailInfo->set_sex(pPlayer->GetAttr(C_GENDER));
	uint32_t dwRuneItemId = 0;
	uint32_t ykRuneItemId = 0;
	pPackage->GetRuneItemId(dwRuneItemId, ykRuneItemId);
	detailInfo->set_dw_rune_id(dwRuneItemId);
	detailInfo->set_yk_rune_id(ykRuneItemId);

/*
	uint32_t* runeItemId = pPackage->GetRuneItemId();
	if (nullptr != runeItemId)
	{
		detailInfo->set_dw_rune_id(runeItemId[EItemRuneType_DW]);
		detailInfo->set_yk_rune_id(runeItemId[EItemRuneType_YK]);
	}*/

	//获取模型信息
	CharFacadeProto* facadeProto = detailInfo->mutable_facade();
	pPackage->GetFacade(*facadeProto);
	detailInfo->mutable_facade()->CopyFrom(*facadeProto);
	//获取装备信息
	::google::protobuf::RepeatedPtrField< ::ProtoBuf::ItemProtoInfo >* equipInfo = detailInfo->mutable_equips();
	pPackage->GetEquipInfo(*equipInfo);
	::google::protobuf::RepeatedPtrField< ::ProtoBuf::EquipSlotProto >* slotsInfo = detailInfo->mutable_slots();
	pPackage->GetEquipSlotInfo(*slotsInfo);
	detailInfo->set_isonline(true);
	detailInfo->set_magdodge(pPlayer->GetAttr(C_MAGDODGE));
	detailInfo->set_teamid(pPlayer->GetAttr(C_TEAM_ID));
	detailInfo->set_maghit(pPlayer->GetAttr(C_MAGHIT));

	return true;
}

bool GQueryManager::QueryCharacterdPetInfo(CharIDType srcId, CharIDType dstId, WCCharacterDetailRsp& rsp)
{
	Player* pPlayer = g_GetCreatureMgr()->GetPlayer(dstId);
	if (nullptr == pPlayer)
	{
		MMOLOG_FMT_ERROR("[logic] GQueryManager::QueryCharacterdPetInfo can not find player playerid:%d", dstId);
		return false;
	}

	return true;
}
